/*
 * @(#)MonitoringAction.java $version 2012-8-10
 * 
 * Copyright 2012 NADOSTAR Gruop. All rights Reserved.
 * NADOSTAR PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nadostar.iims.monitoring.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nadostar.extensions.CommonUtil;
import com.nadostar.extensions.pagination.PaginationInfo;
import com.nadostar.extensions.util.DataMap;
import com.nadostar.iims.etc.bo.CommBO;
import com.nadostar.iims.model.MonitoringParamModel;
import com.nadostar.iims.monitoring.bo.MonitoringBO;
import com.nadostar.iims.util.CommonConst;

/**
 * The Class MonitoringAction.
 * 
 * @author Lixing
 *
 */
 @Controller
 @RequestMapping("/bbs/monitoring")
public class MonitoringAction {
	 private static Log log = LogFactory.getLog(MonitoringAction.class);
	 private static final String KEY_COMMCD_BBS = "BBS";
	 private static PaginationInfo paginate = null;
	 
	 @Autowired
	 private MonitoringBO monitoringBO = null;
	 
	 @Autowired
	 private CommBO commBO = null;
	 
	 @ModelAttribute
	 public void modelPaginateAttr(HttpServletRequest request) {
		 if(paginate == null) {
			 paginate = new PaginationInfo("default", request, 0);
		 }
	 }

	 @ModelAttribute
	 public void modelMonitoringTypeListAttr(Model model) {
		 model.addAttribute("monitoringTypeList", commBO.getCommByCommCdCatList(KEY_COMMCD_BBS));
	 }
	 
	 @RequestMapping(method = RequestMethod.GET)
	 public String execute()  {
		 return "bbs/monitoringList";
	 }
	 
	 @RequestMapping(value = "/search", method = RequestMethod.GET)
	 public @ResponseBody Map<String, ? extends Object> search(
			 @RequestParam("page") int page,
			 @RequestParam("bbsTypeCd") String bbsTypeCd,
			 @RequestParam("trtStat") String trtStat,
			 @RequestParam("startDate") String startDate,
			 @RequestParam("endDate") String endDate,
			 @RequestParam("wrtrMbrId") String wrtrMbrId) {
		 Map<String, Object> jsonMap = new HashMap<String, Object>();
		 
		 MonitoringParamModel model = new MonitoringParamModel();
		 model.setTargetCd(CommonConst.TARGET_OF_ATC);
		 model.setBbsTypeCd(bbsTypeCd);
		 model.setTrtStat(trtStat);
		 model.setStartDate(startDate);
		 model.setEndDate(endDate);
		 model.setWrtrMbrId(wrtrMbrId);
		 
		 log.debug("### MonitoringAction.search...");
		 
		 int total = monitoringBO.getMonitoringListCount(model);
		 paginate.setPage(page);
		 paginate.setTotalRows(total);
		 int offset = CommonUtil.calcOffset(page, paginate.getPageSize());
		 
		 List<DataMap> monitoringList = monitoringBO.getMonitoringList(model, offset, paginate.getPageSize());
		 
		 jsonMap.put("total", total);
		 jsonMap.put("paginate", paginate.getPaginateMap());
		 jsonMap.put("monitoringList", monitoringList);
		 return jsonMap;
	 }
}
